﻿using AutoMapper;
using Harmontronics.DataAccess.IOperate.Business;
using Harmontronics.DataAccess.Operate.Business;
using Harmontronics.IService;
using Harmontronics.IService.Business;
using Harmontronics.Model.Dto;
using Harmontronics.Model.Dto.Model;
using Harmontronics.Model.Entitys;
using Harmontronics.Repository;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Harmontronics.Service
{
    public class RecipeService: IRecipeService
    {
        private readonly IRecipeOperate _recipeOperate;
        private readonly IRecipeTypeOperate _recipeTypeOperate;
        private readonly IMapper _mapper;
        private readonly IUnitOfWork _unitOfWork;
        public RecipeService(IUnitOfWork unitOfWork, IMapper mapper, IRecipeOperate recipeOperate, IRecipeTypeOperate recipeTypeOperate)
        {
            _mapper = mapper;
            _unitOfWork = unitOfWork;
            _recipeOperate = recipeOperate;
            _recipeTypeOperate = recipeTypeOperate;
        }

        public List<RecipeModel> MendingRange(List<Recipe> entities)
        {
            List<RecipeModel> models = new List<RecipeModel>();
            foreach (var entity in entities)
            {
                models.Add(Mending(entity));
            }
            return models;
        }

        public RecipeModel Mending(Recipe entity)
        {
            RecipeModel model = _mapper.Map<RecipeModel>(entity);
            if (!entity.RecipeTypeId.IsNullOrEmpty())
            {
                model.RecipeType = _recipeTypeOperate.GetRecipeTypeById(entity.RecipeTypeId);
            }
            else
            {
                model.RecipeType = new RecipeType() { 
                    RecipeTypeId = entity.RecipeTypeId,
                };
            }
            return model;
        }
        public List<Recipe> GetAllRecipes()
        {
            try
            {
                return _recipeOperate.GetAllRecipeList();
            }
            catch (Exception ex)
            {
                return new List<Recipe>();
            }
        }

        public Recipe GetRecipeById(string id)
        {
            try
            {
                return _recipeOperate.GetRecipeById(id);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public bool addRecipe(Recipe entity)
        {
            try
            {
                _recipeOperate.AddRecipe(entity);
                _unitOfWork.Commit();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool updateRecipe(Recipe model)
        {
            try
            {
                Recipe entity = _recipeOperate.GetRecipeById(model.RecipeId);
                _mapper.Map(model, entity);
                _recipeOperate.UpdateRecipe(entity);
                _unitOfWork.Commit();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool DeleteRecipe(string id)
        {
            try
            {
                Recipe entity = _recipeOperate.GetRecipeById(id);
                if (entity != null)
                {
                    _recipeOperate.DeleteRecipe(entity);
                    _unitOfWork.Commit();
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool BatchDelRecipe(List<string> ids)
        {
            try
            {
                List<Recipe> entities = _recipeOperate.GetRecipeByIdList(ids);
                if (entities.Count > 0)
                {
                    _recipeOperate.BatchDelRecipe(entities);
                    _unitOfWork.Commit();
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
